from typing import Optional
from src.utils.api_request import APIRequest, APIResponse, Dict


class GoodsManage(APIRequest):
    """GoodsManage服务接口类。"""

    def __init__(self, url: str) -> None:
        super().__init__(
            url,
            header={
                "rpc-software": "shop",
                "rpc-transactionid": "123",
                "rpc-service": "shop_goods_manage",
            },
            body={
                "software": "shop",
                "service": "goods_manage",
                "request": dict(),
            },
        )

    def query_goods(
        self,
        goods_name: str,
        page: Optional[int] = None,
        page_size: Optional[int] = None,
    ) -> Dict[APIResponse, None]:
        """
        查询商品。
        :param goods_name: 商品名称
        :param page: 页码
        :param page_size: 页大小
        :return: APIResponse
        """
        self.header["rpc-interface"] = "query_goods"
        self.body["request"].update(goods_name=goods_name)
        self.body["request"].update(page=page)
        self.body["request"].update(pageSize=page_size)
        return self.post()

    def add_goods(
        self, goods_name: str, goods_code: Optional[str] = None
    ) -> Dict[APIResponse, None]:
        """
        添加商品。
        :param goods_code: 商品代码
        :param goods_name: 商品名称
        :return: APIResponse
        """
        self.header["rpc-interface"] = "add_goods"
        self.body["request"].update(goods_name=goods_name)
        self.body["request"].update(goods_code=goods_code)
        return self.post()

    def update_goods(
        self, goods_name: str, goods_code: Optional[str] = None
    ) -> Dict[APIResponse, None]:
        """
        更新商品。
        :param goods_code: 商品代码
        :param goods_name: 商品名称
        :return: APIResponse
        """
        self.header["rpc-interface"] = "update_goods"
        self.body["request"].update(goods_name=goods_name)
        self.body["request"].update(goods_code=goods_code)
        return self.post()

    def delete_goods(self, goods_name: str) -> Dict[APIResponse, None]:
        """
        删除商品。
        :param goods_name: 商品名称
        :return: APIResponse
        """
        self.header["rpc-interface"] = "delete_goods"
        self.body["request"].update(goods_name=goods_name)
        return self.post()
